<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="发放机构">
              <a-select allowClear defaultValue="1" v-model="queryParam.orgIds" showSearch :filter-option="filterOption"
                :not-found-content="null">
                <a-select-option v-for="item in orgList" :key="item.id" :value="item.id">
                  {{ item.departName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="优惠券">
              <a-input-group compact>
                <a-input v-model="queryParam.couponNames" allowClear>
                  <a-icon slot="suffix" type="search" @click.stop="handleSelectCoupon"></a-icon>
                </a-input>
              </a-input-group>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="手机号">
              <a-input placeholder="" v-model="queryParam.mobile" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="优惠券码">
              <a-input placeholder="" v-model="queryParam.couponCode" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="3" :lg="7" :md="8" :sm="24">
            <a-form-item label="状态">
              <a-select v-model="queryParam.bizStatus" allowClear>
                <a-select-option v-for="(item, index) in bizStatus" :key="index" :value="item.value">
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="领取日期">
              <a-range-picker
                :ranges="{ 今天: [moment(), moment()], 近7天: [moment().subtract(7, 'days'), moment()], 本月: [moment().startOf('month'), moment().endOf('month')] }"
                v-model="gainDateRange" @change="handleGainDateRange" />
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="核销日期">
              <a-range-picker
                :ranges="{ 今天: [moment(), moment()], 近7天: [moment().subtract(7, 'days'), moment()], 本月: [moment().startOf('month'), moment().endOf('month')] }"
                v-model="useDateRange" @change="handleUseDateChange" />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" bordered rowKey="id" :columns="columns" :dataSource="dataSource"
        :pagination="ipagination" :loading="loading" class="j-table-force-nowrap" @change="handleTableChange">
        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleDetail(record)">详情</a> -->
        </span>
        <span slot="bizStatus" slot-scope="text">
          <a-tag color="green" v-if="text === 1">已使用</a-tag>
          <a-tag color="" v-if="text === 0">未使用</a-tag>
          <a-tag color="red" v-if="text === -1">作废</a-tag>
          <a-tag color="orange" v-if="text === -2">已过期</a-tag>
        </span>
        <span slot="validTimeDate" slot-scope="text, record">
          {{ `${record.validTime} 至 ${record.endTime}` }}
        </span>
        <!-- validTimeDate -->
      </a-table>
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
    <SelectCouponList ref="selectCouponForm" @ok="selectOk"></SelectCouponList>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { postAction } from '@/api/manage'
import moment from 'moment'
import SelectCouponList from '../member/modules/distCouponStepForm/SelectCouponList'


export default {
  name: 'PosMemberCouponList',
  mixins: [JeecgListMixin],
  components: {
    SelectCouponList
  },
  data() {
    return {
      description: '优惠券发放详情',
      gainDateRange: [],
      useDateRange: [],
      orgList: [],
      columns: [
        {
          title: '手机号',
          align: 'center',
          dataIndex: 'mobile',
        },
        {
          title: '券名称',
          align: 'center',
          dataIndex: 'couponNm',
        },
        {
          title: '发放机构',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '领取时间',
          align: 'center',
          dataIndex: 'getTime',
        },
        {
          title: '优惠类型',
          align: 'center',
          dataIndex: 'couponType',
          customRender: (text, record) => {
            if (text === 'zk') {
              return '折扣'
            }
            if (text === 'mj') {
              return '满减'
            }
          }
        },
        {
          title: '券码',
          align: 'center',
          dataIndex: 'couponCode',
        },
        {
          title: '有效期',
          align: 'center',
          dataIndex: 'validTimeDate',
          scopedSlots: { customRender: 'validTimeDate' },
        },
        {
          title: '状态',
          dataIndex: 'bizStatus',
          align: 'center',
          scopedSlots: { customRender: 'bizStatus' },
        },
        {
          title: '核销时间',
          dataIndex: 'useTime',
          align: 'center',
        },
        {
          title: '核销店铺',
          align: 'center',
          dataIndex: 'useShop',
        },
        {
          title: '订单号',
          align: 'center',
          dataIndex: 'orderNo',
          customRender: (text, record) => {
            return record.orderRespVo ? record.orderRespVo.orderNo : ''
          }
        },
        {
          title: '订单金额',
          align: 'center',
          dataIndex: 'orderAmt',
          customRender: (text, record) => {
            return record.orderRespVo ? record.orderRespVo.orderAmt : ''
          }
        },
        {
          title: '优惠金额',
          align: 'center',
          dataIndex: 'discountAmt',
          customRender: (text, record) => {
            return record.orderRespVo ? '-' + record.orderRespVo.discountAmt : ''
          }
        },

        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   align: 'center',
        //   scopedSlots: { customRender: 'action' },
        // },
      ],
      bizStatus: [
        { text: '未使用', value: '0' },
        { text: '已使用', value: '1' },
        { text: '已过期', value: '-1' },
      ],
      url: {
        list: '/member/memberCoupon/list',
        shopListUrl: '/sys/sysDepart/getMyDepartList',
      },
    }
  },
  computed: {},
  created() {

  },
  methods: {
    moment,
    beforeCreate() {
      this.loadShopList()
    },
    handleGainDateRange(date, dateString) {
      this.queryParam.getBeganTime = dateString[0]
      this.queryParam.getEndTime = dateString[1]
    },
    handleUseDateChange(date, dateString) {
      this.queryParam.useBeganTime = dateString[0]
      this.queryParam.useEndTime = dateString[1]
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    loadShopList() {
      this.orgList = []
      postAction(this.url.shopListUrl + '?type=2').then((res) => {
        if (res.success) {
          this.orgList = res.result
        }
      })
    },
    handleSelectCoupon() {
      this.$refs.selectCouponForm.search(this.queryParam.orgIds)
      this.$refs.selectCouponForm.title = '选择优惠券'
      this.$refs.selectCouponForm.visible = true
    },
    selectOk(result) {
      console.log('result', result)
      if (result && result.length > 0) {
        console.log('callback set value')
        let couponNames = [], couponIds = []
        for (var r of result) {
          couponNames.push(r.actNm)
          couponIds.push(r.couponId)
        }
        this.$set(this.queryParam, 'couponNames', couponNames.join(','))
        this.$set(this.queryParam, 'couponIds', couponIds.join(','))
      }
    },
  },
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>